package no.hig.imt4721.matching;

import java.security.InvalidParameterException;

import no.hig.imt4721.model.Repetition;
import no.hig.imt4721.model.Template;
import no.hig.imt4721.model.TemplateWithAverageDeviation;

public class DistanceMetricAbsoluteAverageDeviation extends DistanceMetric
{
	public double getDistance(Template template, Repetition repetition)
	{
		if(!(template instanceof TemplateWithAverageDeviation))
			throw new InvalidParameterException();
		
		TemplateWithAverageDeviation templateWithSD = (TemplateWithAverageDeviation) template;
		
		double d = 0;
		
		for(int i = 0; i < repetition.getLengthOfRow(); i++)
		{
			d += getSingleFeatureDistance(template.getValue(i), repetition.getRowElement(i), templateWithSD.getAverageDeviationValue(i));
		}
		
		return d;
	}
	
	private double getSingleFeatureDistance(double inputValue, double templateValue, double averageDeviation)
	{
		return Math.abs(inputValue - templateValue) / averageDeviation;
	}
}
